<?php
/**
 * 
 */

class PosisiController extends Zend_Controller_Action
{
    private $_posisi = null;
    
    public function preDispatch() {
        $this->_posisi = new Application_Model_Posisi;
        $this->view->site = $this->getRequest()->getControllerName();
    }
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $posisi = $this->_posisi->getAllData();
        $row = $this->_posisi->checkRowPosisi();
        $this->view->posisi = $posisi;
        if ($row == 0)
          $this->view->notFound = '<div class="notfound">Tidak Ada Data di Halaman Ini</div>';
        else
          $this->view->notFound = '<div class="notfound">Terdapat '.$row.' Data Pada Tabel Ini</div>';
    }

    public function addAction()
    {
        $form = new Application_Form_PosisiForm;
        $this->view->form = $form;
        
        if($this->getRequest()->isPost())
        {
            if($form->isValid($this->_request->getPost())) {
                $nama = $form->getValue('nama_posisi');
                $keterangan = $form->getValue('keterangan_posisi');
                if($this->_posisi->checkPosisi($nama))
                    $this->view->duplicate = '<ul class="errors" style="margin-left:150px;"><li>Nama Posisi Sudah Ada</li></ul>';
                else {
                    $this->_posisi->posisiBaru($nama, $keterangan);
                    $this->_redirect("posisi/index");                    
                }} else {
                    $form->populate($this->_request->getPost());
                }
        }
    }

    public function editAction()
    {
        $form = new Application_Form_PosisiForm;
        $form->tambah->setLabel('Simpan');
        
        $this->view->form = $form;
        
        if($this->getRequest()->isPost())
        {
            if($form->isValid($this->_request->getPost())) {
                $id = $form->getValue('id_posisi');
                $nama = $form->getValue('nama_posisi');
                $keterangan = $form->getValue('keterangan_posisi');
                
                $this->_posisi->posisiUpdate($id, $nama, $keterangan);
                
                $this->_redirect("posisi/index");
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0)
            {
                $form->populate($this->_posisi->getposisi($id));
            }
        }
    }

    public function deleteAction()
    {
        $form = new Application_Form_DeleteForm;
        
        $form->removeElement('id_departemen');
        $form->addElement('hidden', 'id_posisi', 'int');
        
        $form->setAction("posisi/delete");
        
        $this->view->form = $form;
        
        if($this->getRequest()->isPost())
        {
            $del = $this->getRequest()->getPost('del1');
            if($del == 'Ya')
            {
                $this->_posisi->posisiDelete($this->getRequest()->getPost('id_posisi'));
                $this->_redirect("posisi/index");
            }
            else {
                $this->_redirect("posisi/index");
            }
        } else {
           $id = $this->_getParam('id', 0);
            if ($id > 0)
            {
                $form->populate($this->_posisi->getPosisi($id));
            }
        }
    }


}